﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;

public partial class main_uc_company_main : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        if (!Page.IsPostBack)
        {
            int sortby = 1;
            if (Request.QueryString["sortby"] != null && Request.QueryString["sortby"] != "")
            {
                try
                {
                    sortby = Convert.ToInt32(Request.QueryString["sortby"]);
                }
                catch { }
            }
            LoadCompany(sortby);
            ddl_Sortby.Items.Clear();
            ddl_Sortby.Items.Add(new ListItem("Data Newest", "1"));
            ddl_Sortby.Items.Add(new ListItem("Data Oldest", "2"));
            ddl_Sortby.Items.Add(new ListItem("Gold Member", "3"));
            ddl_Sortby.Items.Add(new ListItem("Silver Member", "4"));
            ddl_Sortby.Items.Add(new ListItem("Bronze Member", "5"));
            ddl_Sortby.Items.Add(new ListItem("Member", "6"));
            ddl_Sortby.SelectedValue = sortby.ToString();
        }
    }

    private void LoadCompany(int sortby)
    {
        int page = 1;
        if (Request.QueryString["page"] != null && Request.QueryString["page"] != "")
            Int32.TryParse(Request.QueryString["page"], out page);
        int catid = 0;
        if (Request.QueryString["catid"] != null && Request.QueryString["catid"] != "")
            Int32.TryParse(Request.QueryString["catid"], out catid);
        int ttype = 0;
        if (Request.QueryString["ttype"] != null && Request.QueryString["ttype"] != "")
            Int32.TryParse(Request.QueryString["ttype"], out ttype);
        string name = "";
        if (Request.QueryString["name"] != null && Request.QueryString["name"] != "")
            name = Request.QueryString["name"];
        int countryid = 0;
        if (Request.QueryString["countryid"] != null && Request.QueryString["countryid"] != "")
            Int32.TryParse(Request.QueryString["countryid"], out countryid);
        int start = 1;
        int limit = 15;
        if (page != 1)
        {
            start = limit * (page-1);
            limit = (limit * (page - 1)) + 15;
        }
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SP_GetAllCompanyCount", conn))
            {
                cmd.Parameters.Add("lang", this.LangId);
                cmd.Parameters.Add("start", 1);
                cmd.Parameters.Add("limit", 1000000);
                cmd.Parameters.Add("order", "id");
                cmd.Parameters.Add("orderdir", "asc");
                if (name == "")
                    cmd.Parameters.Add("name", null);
                else cmd.Parameters.AddWithValue("name", "%"+name+"%");
                if(catid==0)
                    cmd.Parameters.Add("catid", null);
                else cmd.Parameters.Add("catid", catid);
                if (ttype == 0)
                    cmd.Parameters.Add("ttype", null);
                else cmd.Parameters.Add("ttype", ttype);
                if (countryid == 0)
                    cmd.Parameters.Add("countryid", null);
                else cmd.Parameters.Add("countryid", countryid);
                if (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["logged"] != null && 
                    DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["logged"]) == "true"
                            && (DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["type"].ToString()) == "admin"))
                    cmd.Parameters.Add("isAdmin", 1);
                else cmd.Parameters.Add("isAdmin", 0);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                object val = cmd.ExecuteScalar();//total records
                if (limit > (int)val)
                {
                    limit = (int)val;
                }
                double pages = Math.Ceiling(Convert.ToDouble(val) / 15);
                HyperLink hl;
                int _pages = (int)pages;
                if (_pages <= 5)
                {
                    for (int i = 1; i <= _pages; i++)
                    {
                        hl = new HyperLink();
                        hl.Text = i.ToString();
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=" + i.ToString();
                        if (i != 1 && i != (_pages + 1))
                            phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);
                        if ((i == pages) && page != _pages)
                        {
                            phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                            hl = new HyperLink();
                            hl.Text = DAO.TextBaseFactory.loadText("next", this.LangId);
                            hl.CssClass = "lblInfo";
                            hl.NavigateUrl = "~/Company.aspx?page=" + (page + 1);
                            phNav.Controls.Add(hl);
                        }
                    }
                }
                else
                {
                    if (_pages - page >= 6)
                    {
                        switch (page)
                        {
                            case 2:
                                hl = new HyperLink();
                                hl.Text = "1";
                                hl.CssClass = "lblInfo";
                                hl.NavigateUrl = "~/Company.aspx?page=1";
                                phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                                phNav.Controls.Add(hl);

                            break;
                            case 3:
                                hl = new HyperLink();
                                hl.Text = "1";
                                hl.CssClass = "lblInfo";
                                hl.NavigateUrl = "~/Company.aspx?page=1";
                                phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                                phNav.Controls.Add(hl);

                                hl = new HyperLink();
                                hl.Text = "2";
                                hl.CssClass = "lblInfo";
                                hl.NavigateUrl = "~/Company.aspx?page=2";
                                phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                                phNav.Controls.Add(hl);

                            break;
                            case 4:
                                hl = new HyperLink();
                                hl.Text = "1";
                                hl.CssClass = "lblInfo";
                                hl.NavigateUrl = "~/Company.aspx?page=1";
                                phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                                phNav.Controls.Add(hl);

                                hl = new HyperLink();
                                hl.Text = "2";
                                hl.CssClass = "lblInfo";
                                hl.NavigateUrl = "~/Company.aspx?page=2";
                                phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                                phNav.Controls.Add(hl);

                                hl = new HyperLink();
                                hl.Text = "3";
                                hl.CssClass = "lblInfo";
                                hl.NavigateUrl = "~/Company.aspx?page=3";
                                phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                                phNav.Controls.Add(hl);
                            break;
                        }
                        if (page > 4)
                        {
                            hl = new HyperLink();
                            hl.Text = "1";
                            hl.CssClass = "lblInfo";
                            hl.NavigateUrl = "~/Company.aspx?page=1";
                            phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                            phNav.Controls.Add(hl);

                            hl = new HyperLink();
                            hl.Text = "2";
                            hl.CssClass = "lblInfo";
                            hl.NavigateUrl = "~/Company.aspx?page=2";
                            phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                            phNav.Controls.Add(hl);

                            hl = new HyperLink();
                            hl.Text = "3";
                            hl.CssClass = "lblInfo";
                            hl.NavigateUrl = "~/Company.aspx?page=3";
                            phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                            phNav.Controls.Add(hl);

                            phNav.Controls.Add(new LiteralControl("<span>|&nbsp;...&nbsp;</span>"));

                            hl = new HyperLink();
                            hl.Text = (page - 1).ToString();
                            hl.CssClass = "lblInfo";
                            hl.NavigateUrl = "~/Company.aspx?page=" + (page - 1).ToString();
                            phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                            phNav.Controls.Add(hl);
                        }

                        hl = new HyperLink();
                        hl.Text = page.ToString();
                        hl.CssClass = "lblInfo-bold";
                        hl.NavigateUrl = "~/Company.aspx?page=" + page.ToString();
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);

                        hl = new HyperLink();
                        hl.Text = (page + 1).ToString();
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=" + (page + 1).ToString();
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);

                        hl = new HyperLink();
                        hl.Text = (page + 2).ToString();
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=" + (page + 2).ToString();
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);
                        phNav.Controls.Add(new LiteralControl("<span>|&nbsp;...&nbsp;</span>"));

                        hl = new HyperLink();
                        hl.Text = (_pages - 2).ToString();
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=" + (_pages - 2).ToString();
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);

                        hl = new HyperLink();
                        hl.Text = (_pages - 1).ToString();
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=" + (_pages - 1).ToString();
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);

                        hl = new HyperLink();
                        hl.Text = _pages.ToString();
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=" + _pages.ToString();
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);

                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        hl = new HyperLink();
                        hl.Text = DAO.TextBaseFactory.loadText("next", this.LangId);
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=" + (page + 1);
                        phNav.Controls.Add(hl);
                    }
                    else
                    {
                        hl = new HyperLink();
                        hl.Text = "1";
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=1";
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);

                        hl = new HyperLink();
                        hl.Text = "2";
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=2";
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);

                        hl = new HyperLink();
                        hl.Text = "3";
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Company.aspx?page=3";
                        phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);

                        phNav.Controls.Add(new LiteralControl("<span>|&nbsp;...&nbsp;</span>"));

                        for (int i = (page-1); i <= _pages; i++)
                        {
                            hl = new HyperLink();
                            hl.Text = i.ToString();
                            if(i==page)
                                hl.CssClass = "lblInfo-bold";
                            else
                                hl.CssClass = "lblInfo";
                            hl.NavigateUrl = "~/Company.aspx?page=" + i.ToString();
                            if (i != 1 && i != (_pages + 1))
                                phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                            phNav.Controls.Add(hl);
                            if ((i == pages) && page != _pages)
                            {
                                phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                                hl = new HyperLink();
                                hl.Text = DAO.TextBaseFactory.loadText("next", this.LangId);
                                hl.CssClass = "lblInfo";
                                hl.NavigateUrl = "~/Company.aspx?page=" + (page + 1);
                                phNav.Controls.Add(hl);
                            }
                        }
                    }
                }
                if ((int)val == 0)
                {
                    lblInfo.Text = DAO.TextBaseFactory.loadText("no_records", this.LangId);
                }
                else lblInfo.Text = DAO.TextBaseFactory.loadText("showing", this.LangId) + " " + start + "-" + limit + " " + DAO.TextBaseFactory.loadText("of", this.LangId) + " " + val + " " + DAO.TextBaseFactory.loadText("total_res", this.LangId);
                cmd.Parameters.Clear();
                cmd.CommandText = "SP_GetAllCompany";
                cmd.Parameters.Add("lang", this.LangId);
                cmd.Parameters.Add("start", start);
                cmd.Parameters.Add("limit", limit);
                cmd.Parameters.Add("order", "id");
                switch (sortby)
                {
                    case 1:
                        cmd.Parameters.AddWithValue("orderdir", "desc");
                        cmd.Parameters.AddWithValue("member_type_id", null);
                        break;
                    case 2:
                        cmd.Parameters.AddWithValue("orderdir", "asc");
                        cmd.Parameters.AddWithValue("member_type_id", null);
                        break;
                    case 3:
                        cmd.Parameters.AddWithValue("orderdir", "desc");
                        cmd.Parameters.AddWithValue("member_type_id", 1);
                        break;
                    case 4:
                        cmd.Parameters.AddWithValue("orderdir", "desc");
                        cmd.Parameters.AddWithValue("member_type_id", 2);
                        break;
                    case 5:
                        cmd.Parameters.AddWithValue("orderdir", "desc");
                        cmd.Parameters.AddWithValue("member_type_id", 3);
                        break;
                    case 6:
                        cmd.Parameters.AddWithValue("orderdir", "desc");
                        cmd.Parameters.AddWithValue("member_type_id", 4);
                        break;
                    default:
                        cmd.Parameters.AddWithValue("orderdir", "desc");
                        cmd.Parameters.AddWithValue("member_type_id", null);
                        break;
                }
                if (name == "")
                    cmd.Parameters.AddWithValue("name", null);
                else cmd.Parameters.AddWithValue("name", "%" + name + "%");
                if (catid == 0)
                    cmd.Parameters.AddWithValue("catid", null);
                else cmd.Parameters.AddWithValue("catid", catid);
                if (ttype == 0)
                    cmd.Parameters.AddWithValue("ttype", null);
                else cmd.Parameters.AddWithValue("ttype", ttype);
                if (countryid == 0)
                    cmd.Parameters.AddWithValue("countryid", null);
                else cmd.Parameters.AddWithValue("countryid", countryid);
                if (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["logged"] != null && 
                    DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["logged"]) == "true"
                            && (DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["type"].ToString()) == "admin"))
                    cmd.Parameters.AddWithValue("isAdmin", 1);
                else cmd.Parameters.AddWithValue("isAdmin", 0);
                cmd.CommandType = System.Data.CommandType.StoredProcedure; 
                
                SqlDataReader dr = cmd.ExecuteReader();
                StringBuilder sb;
                DateTime dt;
                bool _new = false;
                string _cssClass;
                while (dr.Read())
                {
                    DateTime.TryParse(dr["created"].ToString(), out dt);
                    sb = new StringBuilder();
                    sb.Append("<div class=\"company\"><div class=\"company_header\">");
                    if (DateTime.Compare(dt, Utilities.GetStartOfLastWeek()) >= 0)
                    {
                        sb.Append("<div class=\"comp_new\"></div>");
                        _new = true;
                    }
                    string _cname = (dr["country_name"].ToString().Length > 12) ? dr["country_name"].ToString().Substring(0, 12)+"..." : dr["country_name"].ToString();
                    _cssClass = _new ? "comp_header_type" : "comp_header_type1";
                    sb.Append("<div class=\"comp_header_name\">"+dr["name"]+"</div>");
                    switch (dr["member_type_id"].ToString())
                    {
                        case "1":
                            sb.Append("<div class='" + _cssClass + "'>* * * " + DAO.TextBaseFactory.loadText("gold_member", this.LangId) + "</div></div>");
                            break;
                        case "2":
                            sb.Append("<div class='" + _cssClass + "'>* * " + DAO.TextBaseFactory.loadText("silver_member", this.LangId) + "</div></div>");
                            break;
                        case "3":
                            sb.Append("<div class='" + _cssClass + "'>* " + DAO.TextBaseFactory.loadText("bronze_member", this.LangId) + "</div></div>");
                            break;
                        case "0":
                            sb.Append("<div class='" + _cssClass + "'>" + DAO.TextBaseFactory.loadText("_member", this.LangId) + "</div></div>");
                            break;
                        default:
                            sb.Append("<div class='" + _cssClass + "'>" + DAO.TextBaseFactory.loadText("_member", this.LangId) + "</div></div>");
                            break;
                    }
                    sb.Append("<div class=\"company_main\"><div class=\"company_logo\">");
                    sb.Append("<img width=\"53px\" height=\"39px\" src=\"design_img/CompanyLogoDefault.jpg\" class=\"padding_\" /></div>");
                    sb.Append("<div class=\"comp_details1\"><p><strong>" + DAO.TextBaseFactory.loadText("country", this.LangId) + "</strong></p><p style='text-align: left;'><img height='12' width='20' src='images/countries/" + dr["short"].ToString() + "-t.gif' title='" + dr["country_name"].ToString() + "' />&nbsp;&nbsp;" + dr["short"].ToString() + " - " + _cname + "</p></div>");
                    sb.Append("<div class=\"comp_details2\"><p><strong>" + DAO.TextBaseFactory.loadText("city", this.LangId) + "</strong></p><p>" + dr["City"].ToString() + "</p></div>");
                    sb.Append("<div class=\"compType\"><p><strong>" + DAO.TextBaseFactory.loadText("company_type", this.LangId) + "</strong></p>");
                    sb.Append("<p>" + dr["companyType"].ToString() + "</p></div>");
                    sb.Append("<div class=\"compMore\"><div class=\"viewInfoLink\"><a href=\"CompanyDetails.aspx?cid=" + dr["id"].ToString() + "\" class=\"viewInfoLink\" >" + DAO.TextBaseFactory.loadText("comp_info_det", this.LangId) + "</a></div>");
                    sb.Append("</div></div></div>");
                    phCompany.Controls.Add(new LiteralControl(sb.ToString()));
                }
                dr.Close();
            }
        }
        
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }

    protected void ddl_Sortby_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Request.QueryString.Count == 0)
            Response.Redirect(Request.Url.ToString() + "?sortby=" + ddl_Sortby.SelectedValue.ToString());
        else
        {
            if (Request.QueryString["sortby"] != "")
            {
                string q = "~/Company.aspx?";
                foreach (string kv in Request.QueryString)
                {
                    if (kv == "sortby")
                    {
                        q += kv + "=" + ddl_Sortby.SelectedValue.ToString();
                    }
                    else q += kv + "=" + Request.QueryString[kv];
                }
                Response.Redirect(q);
            }
            else Response.Redirect(Request.Url.ToString() + "&sortby=" + ddl_Sortby.SelectedValue.ToString());
        }
        //int sortby = 1;
        //if (Int32.TryParse(ddl_Sortby.SelectedValue.ToString(), out sortby))
        //    LoadCompany(sortby);
    }
}
